package village.rainbow.slipin.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import village.rainbow.slipin.Global;

import android.content.Context;
import android.os.Environment;

public class FileUtil {
	
	public static boolean isDbExists(Context context, String dbFileName){
		String databaseDir = Environment.getDataDirectory() + "/data/" + context.getPackageName() + "/databases";
		File dir = new File(databaseDir);
		if(!dir.exists())
			return false;
		File file = new File(dir + "/" + dbFileName);
		if(!file.exists())
			return false;
		else
			return true;
		
	}
	
	public static void pullDbToSd(Context context) {
		if(FileUtil.isDbExists(context, Global.DB_NAME)){
			try{
				File file = new File(Environment.getDataDirectory() + "/data/" + context.getPackageName() + "/databases/" + Global.DB_NAME);
				FileInputStream is = new FileInputStream(file);
				
				File dir = new File(Environment.getExternalStorageDirectory().getPath() + Global.SDCARD_PATH);
				if(!dir.exists())
					dir.mkdir();
				File outputFile = new File(dir, Global.DB_NAME);
				if(outputFile.exists())
					outputFile.delete();
				outputFile.createNewFile();
			
				FileOutputStream fous = new FileOutputStream(outputFile);
				byte[] buffer = new byte[(int)file.length()];
				is.read(buffer);
				fous.write(buffer);
				is.close();
				fous.close();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}	
	
	
//	public static void writeToTxtFile(String id, String requestParam, String responseParam) {
//		try{
//			File dir = new File(Environment.getExternalStorageDirectory().getPath() + "/mobile_pos");
//			if(!dir.exists())
//				dir.mkdir();
//			File outputFile = new File(dir, "mobile_pos_sample_" + id + ".txt");
//			if(outputFile.exists())
//				outputFile.delete();
//			outputFile.createNewFile();
//		
//			FileOutputStream fous = new FileOutputStream(outputFile);
//			String wholeData = requestParam + "\r\n\r\n\r\n" + responseParam;
//			fous.write(wholeData.getBytes());
//			fous.close();	
//		}catch(Exception e){
//			e.printStackTrace();
//		}
//		
//	}
	
	/*
	public static Drawable getImageFile(Context context, String fileName) throws IOException{
		String imageDir = Environment.getDataDirectory() + "/data/" + context.getPackageName() + "/fdk_images/";
		File dir = new File(imageDir);
		if(!dir.exists())
			return null;
		File imageFile = new File(imageDir + fileName);
		if(!imageFile.exists())
			return null;
		int imageSize = (int)imageFile.length();
		FileInputStream fis = new FileInputStream(imageFile);
		byte[] buffer = new byte[imageSize];
		fis.read(buffer);
		
		Drawable drawable = new BitmapDrawable
		
	}
	*/
	
//	public Drawable getLoginBg(Context context, String filePath) throws IOException{
//		String imageDir = Environment.getDataDirectory() + "/data/" + context.getPackageName() + "/fdk_images/";
//		File dir = new File(imageDir);
//		if(!dir.exists())
//			return null;
//		File imagefile = new File(dir + "/" + filePath);
//		if(!imagefile.exists())
//			return null;
//		
//		Drawable d = BitmapDrawable.createFromPath(dir + "/" + filePath);
//		return d;
//	}
	
	/*
	public static void pullFileToSd(File imageFile, String dir, String fileName ) throws IOException{
		FileInputStream is = new FileInputStream(imageFile);
		File external = new File(Environment.getExternalStorageDirectory().getPath() + "/mobile_pos");
		if(!external.exists())
			external.mkdir();
		File outputFile = new File(external, fileName);
		if(outputFile.exists())
			outputFile.delete();
		outputFile.createNewFile();
	
		FileOutputStream fous = new FileOutputStream(outputFile);
		byte[] buffer = new byte[(int)imageFile.length()];
		is.read(buffer);
		fous.write(buffer);
		is.close();
		fous.close();
	}
	
	public static void writeImageFile(Context context, String fileName, byte[] b) throws IOException{
		String imageDir = Environment.getDataDirectory() + "/data/" + context.getPackageName() + "/fdk_images/";
		File dir = new File(imageDir);
		if(!dir.exists())
			dir.mkdirs();
		File imagefile = new File(dir +"/"+ fileName);
		if(imagefile.exists())
			imagefile.delete();
		imagefile.createNewFile();
		FileOutputStream fous = new FileOutputStream(imagefile);
		fous.write(b);
		fous.flush();
		fous.close();
	}
	
	public static void writeFile(File file, byte[] b) throws IOException {
		FileOutputStream out = new FileOutputStream(file);
		out.write(b);
		out.close();
	}
	
	
	public static byte[] getBytes(String path) throws IOException {
		File file = new File(path);
		long fileSize = file.length();
		byte[] data = new byte[(int)fileSize];
		FileInputStream is = new FileInputStream(file);
		is.read(data);
		is.close();
		
		return data;
	}
	
	public static void writeInTextFile(Exception e){
		try{
			File dir = new File(Environment.getExternalStorageDirectory().getPath() + "/mobile_pos");
			if(!dir.exists())
				dir.mkdir();
			File file = new File(Environment.getExternalStorageDirectory().getPath() + "/mobile_pos/" + "dbio.txt");
			BufferedWriter writer = new BufferedWriter(new FileWriter(file, true));
			writer.append(e.getMessage() + "\r\n\r\n");
			writer.flush();
			writer.close();
		}catch(Exception exception){
			exception.printStackTrace();
		}
	}
	*/

}
